<template>
  <div class="app-container">
    <!--搜索-->
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="120px">

      <el-form-item label="检查日期" prop="checkDateRange">
        <el-date-picker
          v-model="queryParams.checkDateRange"
          size="small"
          type="daterange"
          range-separator="~"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          style="width: 240px"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--按钮区-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-download" size="mini"
                   @click="handleExport">导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="morningList" >
      <el-table-column label="班级" align="center" prop="className" />
      <el-table-column label="学生" align="center" prop="studentNames" />
      <el-table-column label="类型" align="center" prop="type" />
      <el-table-column label="登记人" align="center" prop="registrantUser" />
      <el-table-column label="登记日期" align="center" prop="registrantDate"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listMorningExercises,exportMorningExercises } from "@/api/report/morningexercises";

export default {
  name: "morningExercises",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 字典表格数据
      morningList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        checkDateRange: [new Date().toISOString().split('T')[0], new Date().toISOString().split('T')[0]] // 设置默认值为今天的日期
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      const params = {
        ...this.queryParams,
        startDate: this.queryParams.checkDateRange[0],
        endDate: this.queryParams.checkDateRange[1]
      };
      delete params.checkDateRange;
      listMorningExercises(params).then(response => {
          this.morningList = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (!this.queryParams.checkDateRange[0] || !this.queryParams.checkDateRange[1]) {
        this.$message.error('开始日期和结束日期不能为空');
        return;
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$confirm('是否确认导出早操统计数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        if (!this.queryParams.checkDateRange[0] || !this.queryParams.checkDateRange[1]) {
          this.$message.error('开始日期和结束日期不能为空');
          return;
        }
        const params = {
          ...this.queryParams,
          startDate: this.queryParams.checkDateRange[0],
          endDate: this.queryParams.checkDateRange[1]
        };
        delete params.checkDateRange;
        return exportMorningExercises(params);
      }).then(response => {
        this.fileDownload(response.data, "早操统计.xlsx");
      }).catch(() => {});
    }
  }
};
</script>

